package com.xiashitech.apiservice;

import cn.hutool.extra.spring.SpringUtil;
import com.xiashitech.apiservice.config.AppConfig;
import com.xiashitech.apiservice.grpc.*;
import io.grpc.*;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.retry.annotation.EnableRetry;

import java.util.Set;

import static io.grpc.Metadata.ASCII_STRING_MARSHALLER;

@MapperScan("com.xiashitech.apiservice.dao")
@SpringBootApplication
@EnableDiscoveryClient
@EnableCaching
@EnableRetry
public class ApiserviceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ApiserviceApplication.class, args);
//        new Thread(()->{
//            try {
//                AgentStatusService ass = SpringUtil.getBean(AgentStatusService.class);
//                ApiInfoService ais = SpringUtil.getBean(ApiInfoService.class);
//                ConfigService cs = SpringUtil.getBean(ConfigService.class);
//                UserInfoService uis = SpringUtil.getBean(UserInfoService.class);
//                AuthFactService afs = SpringUtil.getBean(AuthFactService.class);
//                IncrementalCacheService ics = SpringUtil.getBean(IncrementalCacheService.class);
//                if(ass == null || ais == null || cs == null || uis == null || afs == null || ics == null) {
//                    System.out.println("ERROR: Service is null,  " + ass + ", " + ais + ", " + cs + ", " + ", " + uis + ", " + afs + ", " + ics);
//                    return;
//                }
//                AppConfig config = SpringUtil.getBean(AppConfig.class);
//                // 统一的拦截器
//                System.out.println("GrpcServer start.........: " + (config.getServerPort() + 10) + ": " + config.getGrpcPort());
//                int port = config.getGrpcPort();
//                if(port == 0) port = config.getServerPort() + 10;
//                Server server = ServerBuilder.forPort(port)
//                        .addService(ass)
//                        .addService(ais)
//                        .addService(cs)
//                        .addService(uis)
//                        .addService(afs)
//                        .addService(ics)
//                        .build().start();
//                System.out.println(String.format("GRpc服务端启动成功, 端口号: %d.", port));
//                server.awaitTermination();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }).start();
    }
}